package mo.tags.stack;

public class LO33 {

    public boolean verifyPostorder(int[] postorder) {
        return isValid(postorder, 0, postorder.length-1);
    }

    private boolean isValid(int[] arr, int i, int j) {
        if (i >= j) {
            return true;
        }
        int root = arr[j];
        int k = i;
        for (; k < j; k++) {
            if (arr[k] > root) {
                break;
            }
        }
        for (int l = k; l < j; l++) {
            if (arr[l] < root) {
                return false;
            }
        }
        return isValid(arr, i, k-1) && isValid(arr, k, j-1);
    }

    public static void main(String[] args) {
        LO33 test = new LO33();
        System.out.println(test.verifyPostorder(new int[]{1,3,2,6,5}));
    }

}
